package zhc.app.controller;

import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import zhc.app.entity.Sys_menu;
import zhc.app.service.SysMenuService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@IocBean
public class SysHomeController {
    private static final Log log = Logs.get();

    @Inject
    private SysMenuService sysMenuService;

    @At("/home")
    @Ok("beetl:/platform/sys/home.html")
    public void home(HttpServletRequest request) {
        // TODO: 2017/5/6  菜单做缓存 每个用户都查询 耗性能
        List<Sys_menu> menuList=sysMenuService.query(Cnd.where("type","=","menu"));
        Map<String,Sys_menu> menuMap=new HashMap<String, Sys_menu>();
        for (Sys_menu menu:menuList){
            String path=menu.getPath();
            if(path.length()==4){
                if(menuMap.get(path)!=null){
                    menu.setChildren(menuMap.get(path).getChildren());
                    menuMap.put(path,menu);
                }else{
                    menu.setChildren(new ArrayList<Sys_menu>());
                    menuMap.put(path,menu);
                }
            }else if(path.length()>4){
                String parentPath=path.substring(0,4);
                Sys_menu parent=menuMap.get(parentPath);
                if(parent!=null){
                    parent.getChildren().add(menu);
                }else{
                    Sys_menu tempMenu=new Sys_menu();
                    tempMenu.setChildren(new ArrayList<Sys_menu>());
                    tempMenu.getChildren().add(menu);
                    menuMap.put(parentPath,tempMenu);
                }
            }
        }
        request.setAttribute("menuMap",menuMap);
        request.getSession().setAttribute("menuMap",menuMap);
        request.setAttribute("obj","123123");
    }

}
